package com.study.mall.mallproduct.controller;

import com.study.mall.mallproduct.entity.PmsCategory;
import com.study.mall.mallproduct.service.PmsCategoryService;
import com.stydy.mallcommon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-03-05
 */
@RestController
@RequestMapping("/product/pmsCategory")
public class PmsCategoryController {
    @Autowired
    private PmsCategoryService pmsCategoryService;
    //获取分类后的数据
    @GetMapping("/listTree")
    public R list() {
        List<PmsCategory> categories = pmsCategoryService.listWithTree();
        return R.ok().put("data",categories);
    }
    //回显一条数据
    @PostMapping("/info")
    public R info(@RequestBody Long id){
        pmsCategoryService.getById(id);
        return R.ok();
    }
    //修改
    @PostMapping("/edit")
    public R edit(@RequestBody PmsCategory pmsCategory){
        pmsCategoryService.updateById(pmsCategory);
        return R.ok();
    }
    @GetMapping("/list")
    public R list1(){
        return R.ok().put("data",pmsCategoryService.list());
    }
    //逻辑删除
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
        //pmsCategoryService.removeByIds(Arrays.asList(catIds));
        pmsCategoryService.removeMenuByIds(catIds);
        return R.ok();
    }
    //添加分类
    @PostMapping("/save")
    public R update(@RequestBody PmsCategory pmsCategory){
        pmsCategoryService.save(pmsCategory);
        return R.ok();
    }
    //批量修改排序
    @PostMapping("/updateSort")
    public R updatebyIds(@RequestBody PmsCategory[] pmsCategories){
        this.pmsCategoryService.updateBatchById(Arrays.asList(pmsCategories));
        return R.ok();
    }
    //批量删除
    @PostMapping("/batchDelete")
    public R batchDelete(@RequestBody Long[] ids){
        this.pmsCategoryService.removeMenuByIds(ids);
        return R.ok();
    }

//    @GetMapping("/testList")
//    public List<PmsCategory> testTree(){
//        return pmsCategoryService.testList();
//    }
}

